package com.viking.util.database.entry;

import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.time.LocalDateTime;
@Slf4j

public class EntrySampleDataBase {
    private static ResultSet resultSet;
    private static int Max;

    public static int getMax() throws Exception {
        try{
            Class.forName ("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username="testing";
            String password="Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url,username,password);
//        4.执行SQL对象Statement，执行SQL的对象

            Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet= stmt.executeQuery("SELECT COUNT('*') FROM enterinformation");
            while(resultSet.next())
            {
                Max=resultSet.getInt(1);
            }
            resultSet.close();
            connection.close();
        }catch (Exception e){
            LocalDateTime dt = LocalDateTime.now();
            log.error(dt+"Failed to get SampleDataBase quantity");
        }

        return Max;

    }

    public static void EnterInfo( String yangbenbianhao,String yangbenlaiyuan,String binglihao,String shiyanbianhao,
                                  String qujingfangshi,String jinyufangshi,String shouyangshijian,
                                  String jiancexiangmu,String shenqingyisheng,String yishengdianhua,
                                  String danweidizhi,String shenqingriqi,String yiyuanmingzi,String wuliugongsi,
                                  String wuliudanhao,String baochunfangshi,String quyangshijian,String yangbentiji,
                                  String yehuashijian,String yangbenzhiliangleixing,String yangbenxidishijian,
                                  String xishihoumidu,String xitongyouxuanjingzishuliang,String shijiyouxuanjingzishuliang,String changjialianxifangshi,
                                  String beizhu
                                  ) throws  Exception{
        try
        {
            Class.forName ("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username="testing";
            String password="Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url,username,password);
            String sql = "insert into sampledata (样本编号,样本来源" +
                    ",病历号,实验编号,取精方式,禁欲方式,收样时间,检测项目,申请医生,医生电话,单位地址,申请日期,医院名字,物流公司,物流单号,保存方式," +
                    "取样时间,样本体积,液化时间,样本质量类型,样本洗涤时间,稀释后密度,系统优选精子数量,实际优选精子数量,厂家联系方式,备注) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,yangbenbianhao);//样本编号
            ps.setString(2,yangbenlaiyuan);//样本来源
            ps.setString(3,binglihao);//病历号
            ps.setString(4,shiyanbianhao);//实验编号
            ps.setString(5,qujingfangshi);//取精方式
            ps.setString(6,jinyufangshi);//禁欲时间
            ps.setString(7,shouyangshijian);//收样时间
            ps.setString(8,jiancexiangmu);//检测项目
            ps.setString(9,shenqingyisheng);//申请医生
            ps.setString(10,yishengdianhua);//医生电话
            ps.setString(11,danweidizhi);//单位地址
            ps.setString(12,shenqingriqi);//申请日期
            ps.setString(13,yiyuanmingzi);//医院名字
            ps.setString(14,wuliugongsi);//物流公司
            ps.setString(15,wuliudanhao);//物流单号
            ps.setString(16,baochunfangshi);//保存方式
            ps.setString(17,quyangshijian);//取样时间
            ps.setString(18,yangbentiji);//样本体积
            ps.setString(19,yehuashijian);//液化时间
            ps.setString(20,yangbenzhiliangleixing);//样本质量类型
            ps.setString(21,yangbenxidishijian);//样本洗涤时间
            ps.setString(22,xishihoumidu);//稀释后密度
            ps.setString(23,xitongyouxuanjingzishuliang);//系统优选精子数量
            ps.setString(24,shijiyouxuanjingzishuliang);//实际优选精子数量
            ps.setString(25,changjialianxifangshi);//厂家联系方式
            ps.setString(26,beizhu);//备注
            ps.executeUpdate();
            // 关闭数据库连接对象
            connection.close();
        }
        catch (Exception e)
        {
            LocalDateTime dt = LocalDateTime.now();
            log.error(dt+"Failed to entry Sample database");
        }


    }
}
